package com.ruoyi.jjkc.jjkccg.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.jjkc.jjkccg.domain.Jjkccg;
import com.ruoyi.jjkc.jjkccg.domain.JjkccgFj;
import com.ruoyi.jjkc.jjkccg.mapper.JjkccgMapper;
import com.ruoyi.jjkc.jjkccg.service.IJjkccgService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 加计扣除成果Controller
 *
 * @author huacai
 * @date 2024-12-25
 */
@RestController
@RequestMapping("/jjkc/jjkccg")
public class JjkccgController extends BaseController {
    @Resource
    private IJjkccgService jjkccgService;

    @Resource
    private JjkccgMapper jjkccgMapper;

    /**
     * 查询加计扣除成果列表
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jjkccg:list')")
    @GetMapping("/list")
    public TableDataInfo list(Jjkccg jjkccg) {
        startPage();
        List<Jjkccg> list = jjkccgService.selectJjkccgList(jjkccg);
        return getDataTable(list);
    }

    /**
     * 导出加计扣除成果列表
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jjkccg:export')")
    @Log(title = "加计扣除成果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Jjkccg jjkccg) {
        List<Jjkccg> list = jjkccgService.selectJjkccgList(jjkccg);
        ExcelUtil<Jjkccg> util = new ExcelUtil<Jjkccg>(Jjkccg.class);
        util.exportExcel(response, list, "加计扣除成果数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Jjkccg> util = new ExcelUtil<Jjkccg>(Jjkccg.class);
        util.importTemplateExcel(response, "加计扣除成果数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "加计扣除成果", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('jjkc:jjkccg:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Jjkccg> util = new ExcelUtil<Jjkccg>(Jjkccg.class);
        InputStream inputStream = file.getInputStream();
        List<Jjkccg> list = util.importExcel(inputStream);
        inputStream.close();
        int count = jjkccgService.batchInsertJjkccg(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取加计扣除成果详细信息
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jjkccg:query')")
    @GetMapping(value = "/{jjkccgId}")
    public AjaxResult getInfo(@PathVariable("jjkccgId") String jjkccgId) {
        return success(jjkccgService.selectJjkccgByJjkccgId(jjkccgId));
    }

    /**
     * 新增加计扣除成果
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jjkccg:add')")
    @Log(title = "加计扣除成果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Jjkccg jjkccg) {
        List<Jjkccg> jjkccgList = jjkccgMapper.selectYyNdList(); //已有的年度
        for (Jjkccg jjkccgYyNd : jjkccgList) {
            String yyNd = jjkccgYyNd.getNd(); //已有年度
            if (jjkccg.getNd().equals(yyNd)) {
                return error("该加计扣除" + jjkccg.getNd() + "年成果已存在！");
            }
        }
        return toAjax(jjkccgService.insertJjkccg(jjkccg));
    }

    /**
     * 修改加计扣除成果
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jjkccg:edit')")
    @Log(title = "加计扣除成果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Jjkccg jjkccg) {
        return toAjax(jjkccgService.updateJjkccg(jjkccg));
    }

    /**
     * 删除加计扣除成果
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jjkccg:remove')")
    @Log(title = "加计扣除成果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jjkccgIds}")
    public AjaxResult remove(@PathVariable String[] jjkccgIds) {
        return toAjax(jjkccgService.deleteJjkccgByJjkccgIds(jjkccgIds));
    }

    /**
     * 获得加计扣除成果主键ID (UUID)
     */
    @GetMapping("/getJjKcCgId")
    public AjaxResult getJjKcCgId() {
        String getJjKcCgId = IdUtils.fastSimpleUUID();
        return success(getJjKcCgId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertJjkccgFjByJjkccgId(@RequestBody JjkccgFj jjkccgFj) {
        return toAjax(jjkccgService.insertJjkccgFjByJjkccgId(jjkccgFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectJjkccgFjList(String sId, String fileType) {
        JjkccgFj jjkccgFj = new JjkccgFj();
        jjkccgFj.setJjkccgId(sId);
        jjkccgFj.setFileType(fileType);
        return AjaxResult.success(jjkccgService.selectJjkccgFjList(jjkccgFj));
    }

    /**
     * 删除加计扣除成果附件
     */
    @Log(title = "加计扣除成果附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteJjkccgFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(jjkccgService.deleteJjkccgFjByFjIds(fjIds));
    }

}
